package com.example.it_com.controller;

import com.example.it_com.common.Result;
import com.example.it_com.entity.Announcement;
import com.example.it_com.service.AnnouncementService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/announcement")
@Slf4j
public class AnnouncementController {
    @Autowired
    private AnnouncementService announcementService;

    //公告发布
    @PostMapping("/publishannouncement")
    public Result<String> publishannouncement(Integer userId, String aTitle, String aContent) {
        log.info(userId.toString());
        log.info(aTitle);

        Announcement announcement = new Announcement();
        announcement.setUserId(userId);
        announcement.setATitle(aTitle);
        announcement.setAContent(aContent);
        announcement.setACreatedat(LocalDateTime.now());

        if (announcementService.save(announcement)) {
            return Result.success("", "公告发布成功");
        } else {
            return Result.error("", "公告发布失败");
        }

    }



    // 查看所有公告
    @PostMapping("/getallannouncement")
    public Result<List<Announcement>> getallannouncement() {
        log.info("查看所有公告");

        List<Announcement> announcements = announcementService.list();
        return Result.success(announcements, "查看所有公告成功");
    }


}
